﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Collections;
using System.Web.Script.Serialization;
using System.Web.Mvc;
using System.Web.Http;

namespace FClient.Controllers
{
    public class YahooController : ApiController
    {
        public HttpResponseMessage Get(string username, string password)
        {
            HttpResponseMessage res = new HttpResponseMessage();
            try
            {
                username = username.ToLower();
                YahooApi yahooApi = new YahooApi(username, password);
                bool resultRequest = yahooApi.fetch_request_token();

                if (resultRequest)
                {
                    bool isAccess = yahooApi.GetToken();
                    if (isAccess)
                    {
                        Hashtable infoLogin = yahooApi.SignOn("2", "hehe");
                        if (infoLogin != null)
                        {
                            JavaScriptSerializer js = new JavaScriptSerializer();
                            res.Content = new StringContent(js.Serialize(infoLogin));

                            return res;
                        }
                    }
                }
                return null;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public HttpResponseMessage Get(string action, string sid, string oauth_token, string oauth_token_secret, string seq)
        {
            HttpResponseMessage res = new HttpResponseMessage();
            YahooApi yahooApi = new YahooApi();
            oauth_token = oauth_token.Replace("=", "%3D");
            switch (action)
            {
                case "contact":
                    string result = yahooApi.FetchContactList(oauth_token_secret, oauth_token, sid);
                    res.Content = new StringContent(result);
                    return res;
                case "getnotifycation":
                    string resultNotify = yahooApi.FetchNotification(oauth_token_secret, oauth_token, sid, int.Parse(seq));
                    res.Content = new StringContent(resultNotify);
                    return res;
            }
            return null;
        }

        public HttpResponseMessage Post(string action, string oauth_token, string oauth_token_secret, string sid, string fid, string message)
        {
            YahooApi yahooApi = new YahooApi();
            oauth_token = oauth_token.Replace("=", "%3D");
            HttpResponseMessage res = new HttpResponseMessage();
            JavaScriptSerializer js = new JavaScriptSerializer();

            switch (action)
            {
                case "sendmessage":
                    bool result = yahooApi.SendMessage(fid, message, oauth_token_secret, oauth_token, sid);
                    res.Content = new StringContent(result.ToString());
                    res.StatusCode = HttpStatusCode.OK;
                    return res;
            }
            return null;
        }
    }
}
